import { createDB, randomId, resultFomat, createTime } from './index.js';
import { getDataById } from './getDataById.js';
import { updateDB } from './updateDB.js';
import { addData } from './addData.js';
import { getDataAll } from './getDataAll.js';

export const storeName = 'log';

/**
 * 记录访问次数
 * @param {*} id 
 * @param { string } typeName 分类名称
 * @returns 
 */
export async function visitTimes(id , typeName) {
    const db = await createDB(storeName);
    const data_ = await getDataById(db, storeName, id);
    if (data_) {
        data_.num++;
        return await updateDB(db, storeName, data_);
    } else {
        return await addData(db,storeName,{id,num:1,createTime:createTime() , typeName})
    }
}

/**
 * 获取所有访问次数数据
 * @returns 
 */
export async function getVisitTimesDataAll() {
    const db = await createDB(storeName);
    const data_ = await getDataAll(db, storeName);
   return resultFomat(data_,'rows')
}

/**
 * 根据id查询对应的访问次数
 * @param  { * } id
 * @returns 
 */
export async function getVisitTimesDataById(id) {
    const db = await createDB(storeName);
    const data_ = await getDataById(db, storeName , id);
   return resultFomat(data_,'data')
}
